const ipUtil = require("../../utils/hostUtil")
const requestUtil = require("../../utils/requestUtil")
Page({
    data: {
		showShare: false,
        sku: "",
        fruit: "",
        id: "",
        choose: false,
        oprateType: "",
        imgArr: [],
        collect: false,
        skuList: {
            data: [],
            checkedid: "",
            sku: ""
        }
    },
	onClick(event) {
	    this.setData({ showShare: true });
	  },
	
	  onClose() {
	    this.setData({ showShare: false });
	  },
	cart(){
		wx.switchTab({
		  url: '/pages/cart/cart',
		})
	},
	  onSelect(event) {
		  var id = this.data.id
		  var token= wx.getStorageSync('token')
		  setTimeout(function(){
		      wx.request({ 
		          url: `${ipUtil.host}${requestUtil.share}`,
		          header: requestUtil.header,
		          header:{'content-type': 'application/x-www-form-urlencoded'},
		          method:'POST',
		          data: {token: token},
		          success:function(res){
		              console.log(res)
		          }
		      })
		  },500)
		  return{
		      title:'转发',
		      path:'/pages/detail/detail?id='+id,
		      
		  }
	    this.onClose();
	  },
    onLoad: function (options) {
        wx.setNavigationBarTitle({title: '商品详情'})
        // 商品id
        var id = options.id;
        this.setData({
            id: id
        })
        this.loadPage(id);
		console.log(this.data)
    },
    /**
     * 获取购物车详情
     * @param id
     */
    loadPage: function (id) {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.goods_detail}`,
            data: {fruitId: id,token:wx.getStorageSync("token")},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.data) {
                    var array = [];
                    var data1 = res.data.data;
                    data1.skuList.forEach((item, index) => {
                        if (index <= 4)
                            array.push(item.mainImg);
                    })
                    that.setData({
                        imgArr: array,
                        skuList: {
                            data: data1.skuList,
                            checkedid: "",
                            sku: ""
                        },
                        fruit: data1
                    })
                }
            }
        })
        wx.request({
            url: `${ipUtil.host}${requestUtil.collection}`,
            data: {fruitId: that.data.id, token: wx.getStorageSync("token")},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                that.setData({
                    collect: res.data.msg == "已收藏"
                })
            }
        })
    },
    /**
     * 添加到收藏
     * @param e
     */
    collect: function (e) {
        let that = this;
        let id = this.data.id;
        // collect then del
        if (this.data.collect) {
            wx.request({
                url: `${ipUtil.host}${requestUtil.collection_del}`,
                data: {ids:[id],token:wx.getStorageSync("token")},
                method: "POST",
                header: requestUtil.header,
                success(res) {
                    if (res.data.code == 0) {
                        wx.showToast({
                            title: "取消收藏",
                            icon: "success",
                            duration: 2000
                        })
                        that.setData({
                            collect: !that.data.collect
                        })
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: "none",
                            duration: 2000
                        })
                    }
                }
            })
            return
        }
        wx.request({
            url: `${ipUtil.host}${requestUtil.collection_add}`,
            data: {fruitId: this.data.id, token: wx.getStorageSync("token")},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    wx.showToast({
                        title: "添加到收藏成功",
                        icon: "success",
                        duration: 2000
                    })
                    that.setData({
                        collect: !that.data.collect
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none",
                        duration: 2000
                    })
                }
            }
        })
    }
    /**
     * 关闭选择规则面板
     */
    , closeTemplateAndOprate: function () {
        this.setData({
            choose: false
        })
        var oprateType = this.data.oprateType;
        var checkedid = this.data.skuList.checkedid;
        if (checkedid == "" || checkedid == undefined) {
            wx.showToast({
                title: "请选择规格",
                icon: "none",
                duration: 2000
            })
            return
        }
        if (oprateType != "") {
            if (oprateType == "choose") {
                return
            } else if (oprateType == "to_cart") {
                // 添加到购物车
                this.to_cart_fun()
            } else if (oprateType == "to_order") {
                // 立即购买
                this.to_order_fun()
            }
        }
    },
    closeTemplate: function () {
        this.setData({
            choose: false
        })
    },
    /**
     * 修改选中的sku
     */
    changeChecked: function (e) {
        var skuId = e.currentTarget.dataset.skuid;
        var data = this.data.skuList.data;
        data.forEach(item => {
            if (item.skuId == skuId) {
                this.setData({
                    skuList: {
                        data: this.data.skuList.data,
                        checkedid: skuId,
                        sku: item
                    }, sku: item
                })
            }
        })
    },
    to_cart: function () {
        if (this.data.sku == "") {
            this.setData({
                oprateType: "to_cart",
                choose: true
            })
        } else {
            this.to_cart_fun();
        }
    },
    to_order: function () {
        let token=wx.getStorageSync('token')
        if(!token){
            wx.showToast({
              title: '请登录',
              icon: "none",
              duration: 2000
            })
        }
        if (this.data.sku == "") {
            this.setData({
                oprateType: "to_order",
                choose: true
            })
        } else {
            this.to_order_fun();
        }

    },
    /**
     * 添加购物车
     */
    to_cart_fun: function () {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.addcart}`,
            data: {skuId: this.data.skuList.checkedid, token: wx.getStorageSync("token")},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    wx.showToast({
                        title: "成功添加到购物车",
                        icon: "success",
                        duration: 1500
                    })
                    that.setData({
                        choose: false
                    })
                    // wx.switchTab({
                    //   url: '/pages/cart/cart',
                    // })
                }else{
						wx.showToast({
							title: '请登录',
							icon: "none",
							duration: 2000
						})
				}
            }
        })
    },
    /**
     * 提交订单
     */
    to_order_fun: function () {
        var that = this
        wx.request({
            url: `${ipUtil.host}${requestUtil.buy_now}`,
            data: {
                token: wx.getStorageSync("token"), skuId: this.data.skuList.checkedid
            },
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    that.setData({
                        choose: false
                    })
                    wx.navigateTo({
                        url: `/pages/order/suborder?id=${res.data.data.orderId}&type=buynow`
                    })
                } else {
                    wx.showToast({
                        title: "生成订单失败",
                        icon: "none",
                        duration: 2000
                    })
                }
            }
        })
    },
    /**
     * 仅仅选择规格 而不做任何操作
     */
    choose_without_oprate: function () {
        this.setData({
            oprateType: "choose",
            choose: true
        })
    },
	onPullDownRefresh: function () {
		
	},
	allreview:function(e){
		let id = e.currentTarget.dataset.id
		console.log(id)
		wx.navigateTo({
			url:"/pages/allreview/allreview?fruitId="+id
		})
	}
});